import router from './router';
import store from './store';
import axios from 'axios';
import ElementUI from 'element-ui';
import 'element-ui/lib/theme-chalk/index.css';

axios.defaults.baseURL = "http://localhost:8081";

//axios 前置（请求）拦截
axios.interceptors.request.use(config => {
    console.log(config);
    return config;
});

//后置（响应）拦截
axios.interceptors.response.use(
    response => {
        let res = response.data;
        console.log("------------");
        console.log(res);
        if (res.code === 200) {
            return response;
        } else {
            ElementUI.Message({
                showClose: true,
                message: res.msg,
                type: 'error'
            });
            return Promise.reject(res.msg);
        }
        console.log("------------");

        return response;
    },
    error => {
        console.log("=========");
        console.log(error);
        if (error.response.data) {
            error.message = error.response.data.msg;
        }
        store.commit("REMOVE_INFO");
        ElementUI.Message({
            showClose: true,
            message: error.message,
            type: 'error'
        });
        return Promise.reject(error.message);
    }
);